package com.sdhs.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdhs.common.core.domain.entity.SysUser;
import com.sdhs.common.core.domain.model.LoginUser;
import com.sdhs.common.service.BaseServiceExt;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.common.utils.TextUtil;
import com.sdhs.system.domain.SysNotice;
import com.sdhs.system.mapper.SysNoticeMapper;
import com.sdhs.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 公告 服务层实现
 * 
 * @author engineering
 */
@Service
public class SysNoticeServiceImpl extends BaseServiceExt<SysNoticeMapper,SysNotice> implements ISysNoticeService
{
    @Autowired
    private SysNoticeMapper noticeMapper;

    /**
     * 查询公告信息
     * 
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     * 
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        String userId = user.getUserId();
        notice.setUserId(userId.toString());
        String readStatus = notice.getReadStatus();
        if(TextUtil.isNull(readStatus)){
            notice.setReadStatus("3");
        }
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice)
    {
        return noticeMapper.insertNotice(notice);
    }

    /**
     * 修改公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice)
    {
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     * 
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId)
    {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     * 
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds)
    {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }

    @Override
    public List<SysNotice> listUnRead() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        String userId = user.getUserId();
        List<SysNotice> sysNotices = noticeMapper.listUnRead(userId.toString());
        return sysNotices;
    }
    @Override
    public String listUnReadId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        String userId = user.getUserId();
        List<String> sysNotices = noticeMapper.listUnReadId(userId.toString());
        String ids="";
        if(null !=sysNotices && sysNotices.size()>0){
            ids=String.join(",",sysNotices);
        }
        return ids;
    }
    @Override
    public Integer listUnReadSize() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        String userId = user.getUserId();
        Integer size = noticeMapper.listUnReadSize(userId.toString());
        return size;
    }

    @Override
    public boolean readNotice(Long noticeId) {
        QueryWrapper<SysNotice> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("notice_id",noticeId);
        SysNotice sysNotice = noticeMapper.selectOne(queryWrapper);
        String readUserId = sysNotice.getReadUserId();
        StringBuilder userId=new StringBuilder();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        String loginUserId = user.getUserId();
        if(TextUtil.isNotNull(readUserId)){
            //判断是否有已读
            userId.append(readUserId).append(",").append(loginUserId);
        }else{
            userId.append(loginUserId);
        }
//        Set<String> ids=new HashSet<>(Arrays.asList(userId.toString().split(",")));
        sysNotice.setReadUserId(userId.toString());
        sysNotice.setUserId(loginUserId.toString());
        sysNotice.setUpdateBy(user.getNickName());
        sysNotice.setUpdateTime(new Date());
        int i = this.noticeMapper.updateById(sysNotice);
        if(i==1){
            return true;
        }
        return false;
    }

    /**
     * 批量读取通知消息
     * @param noticeId
     * @return
     */
    public boolean readBatchNotice(String noticeId){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        String loginUserId = user.getUserId();
        List<SysNotice> list=new ArrayList<>();
        if(TextUtil.isNotNull(noticeId)){
            if(noticeId.indexOf(",")!=-1){
                //多条消息
                List<String> ids = Arrays.asList(noticeId.split(","));
                QueryWrapper<SysNotice> queryWrapper=new QueryWrapper<>();
                for(String id:ids){
                    queryWrapper.eq("notice_id",Long.parseLong(id));
                    SysNotice sysNotice = noticeMapper.selectOne(queryWrapper);
                    queryWrapper.clear();
                    String readUserId = sysNotice.getReadUserId();
                    StringBuilder userId=new StringBuilder();
                    if(TextUtil.isNotNull(readUserId)){
                        //判断是否有已读
                        userId.append(readUserId).append(",").append(loginUserId);
                    }else{
                        userId.append(loginUserId);
                    }
//        Set<String> ids=new HashSet<>(Arrays.asList(userId.toString().split(",")));
                    sysNotice.setReadUserId(userId.toString());
                    sysNotice.setUserId(loginUserId.toString());
                    sysNotice.setUpdateBy(user.getNickName());
                    sysNotice.setUpdateTime(new Date());
                    list.add(sysNotice);

                }
                boolean b = this.updateBatchById(list);
                return b;
            }else{
                //只更新一条
              return   readNotice(Long.parseLong(noticeId));
            }
        }
        return false;
    }
}
